package {{package}};

{{#imports}}import {{import}};
{{/imports}}
{{#isJava8or11}}
import com.fasterxml.jackson.databind.ObjectMapper;
{{/isJava8or11}}
import io.swagger.annotations.*;
{{#isJava8or11}}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
{{/isJava8or11}}
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
{{#isJava8or11}}
import java.io.IOException;
{{/isJava8or11}}

{{#isJava8or11}}
{{#jakarta}}
import jakarta.servlet.http.HttpServletRequest;
{{/jakarta}}
{{^jakarta}}
import javax.servlet.http.HttpServletRequest;
{{/jakarta}}
{{/isJava8or11}}
import java.util.List;
{{#isJava8or11}}
import java.util.Optional;
{{/isJava8or11}}
{{^isJava8or11}}
    {{#useOptional}}
import java.util.Optional;
    {{/useOptional}}
{{/isJava8or11}}
{{#async}}
import java.util.concurrent.{{^jdk8}}Callable{{/jdk8}}{{#isJava8or11}}CompletableFuture{{/isJava8or11}};
{{/async}}

{{#operations}}
/**
 * A delegate to be called by the {@link {{classname}}Controller}}.
 * Implement this interface with a {@link org.springframework.stereotype.Service} annotated class.
 */
{{>generatedAnnotation}}
public interface {{classname}}Delegate {
{{#isJava8or11}}

    Logger log = LoggerFactory.getLogger({{classname}}.class);

    default Optional<ObjectMapper> getObjectMapper() {
        return Optional.empty();
    }

    default Optional<HttpServletRequest> getRequest() {
        return Optional.empty();
    }

    default Optional<String> getAcceptHeader() {
        return getRequest().map(r -> r.getHeader("Accept"));
    }
{{/isJava8or11}}

{{#operation}}
    /**
     * @see {{classname}}#{{operationId}}
     */
    {{#isJava8or11}}{{#defaultInterfaces}}default {{/defaultInterfaces}}{{/isJava8or11}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{^isFile}} {{>optionalDataType}} {{/isFile}}{{#isFile}}MultipartFile{{/isFile}} {{paramName}}{{#hasMore}},
        {{/hasMore}}{{/allParams}}){{^isJava8or11}};{{/isJava8or11}}{{#isJava8or11}}{{#defaultInterfaces}} {
        if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
        {{#examples}}
            if (getAcceptHeader().get().contains("{{{contentType}}}")) {
                try {
                    return {{#async}}CompletableFuture.completedFuture({{/async}}new ResponseEntity<>(getObjectMapper().get().readValue("{{#lambdaRemoveLineBreak}}{{#lambdaEscapeDoubleQuote}}{{{example}}}{{/lambdaEscapeDoubleQuote}}{{/lambdaRemoveLineBreak}}", {{>exampleReturnTypes}}.class), HttpStatus.NOT_IMPLEMENTED){{#async}}){{/async}};
                } catch (IOException e) {
                    log.error("Couldn't serialize response for content type {{{contentType}}}", e);
                    return {{#async}}CompletableFuture.completedFuture({{/async}}new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR){{#async}}){{/async}};
                }
            }
        {{/examples}}
        } else {
            log.warn("ObjectMapper or HttpServletRequest not configured in default {{classname}} interface so no example is generated");
        }
        return {{#async}}CompletableFuture.completedFuture({{/async}}new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED){{#async}}){{/async}};
    }{{/defaultInterfaces}}{{/isJava8or11}}

{{/operation}}
}
{{/operations}}
